package cn.shangqizhenshuai.core;

import cn.shangqizhenshuai.entity.ColumnEntity;
import cn.shangqizhenshuai.entity.JavaEntity;
import cn.shangqizhenshuai.entity.MapperEntity;
import cn.shangqizhenshuai.entity.TableEntity;
import cn.shangqizhenshuai.util.Util;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 添加mapper接口文件
 */
public class MapperExecuteCore extends ExecuteCore{

    public boolean isdetailed;

    /**
     * 每次执行完成后需要把信息添加到 上下文中
     */
    public HashMap<String,List>  execute(HashMap<String,List> context) throws IOException, TemplateException {

        //从上下文中获取表信息
        List<TableEntity> selecttabcolumn = context.get("tabinfo");
        // 实体类信息
        List<JavaEntity > entityinfo = context.get("entityinfo");
        //配置模板文件对象
        Configuration conf=new Configuration();
        //导入模板目录
        File file = new File("src/main/resources/");
        conf.setDirectoryForTemplateLoading(file);
        Template temp = conf.getTemplate(this.getFTLname());
        //开始文件输出
        //开始文件输出
        String packagename = this.getPackagename();
        String[] split = packagename.split("\\.");
        String outpath = "src/main/java/"+this.getOutFilePath();
        for (String s : split) {
            outpath +="/"+s;
        }
        File file1 = new File(outpath);
        file1.mkdirs();

        List<MapperEntity> mapperEntities = new ArrayList<MapperEntity>();
        for (JavaEntity javaEntity : entityinfo) {
            //开始循环输出文件
            Writer out=new FileWriter(new File(outpath+"/"+javaEntity.getJavaname()+"Mapper.java"),true);
                       ArrayList<String> arrayList = new ArrayList<String>();
            arrayList.add(javaEntity.getImPackage()+"."+javaEntity.getJavaname());
            arrayList.add(javaEntity.getImPackage()+"."+javaEntity.getJavaname()+"Example");

            MapperEntity mapperEntity = new MapperEntity();
            mapperEntity.setIsdetailed(isdetailed);
            mapperEntity.setImPackage(packagename);
            mapperEntity.setImports(arrayList);
            mapperEntity.setColumnnameandtypemap(javaEntity.getColumnnameandtypemap());
            mapperEntity.setJavaname(javaEntity.getJavaname());
            mapperEntity.setJavaname_tabname_map(javaEntity.getJavaname_tabname_map());
            mapperEntity.setTableEntity(javaEntity.getTableEntity());
            mapperEntity.setTabname_javaname_map(javaEntity.getTabname_javaname_map());
            mapperEntities.add(mapperEntity);
            temp.process(mapperEntity, out);
            out.flush();
            out.close();
        }
        context.put("mapperinfo",mapperEntities);
        return context;
    }


    public boolean isIsdetailed() {
        return isdetailed;
    }

    public void setIsdetailed(boolean isdetailed) {
        this.isdetailed = isdetailed;
    }
}
